<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */

class SomeClient extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->issetSession('SomeClient');
        $this->load->model('SomeClientModel');
        $this->load->model('ClientModel');
        $this->load->helper('download');
    }
    
    public function index($id=false, $printclient=false)
    {
        if($id==false)
        {
            redirect('/client/', 'refresh');
        }
        $client_data = $this->SomeClientModel->getClientData($id);
        $report_data = $this->SomeClientModel->getReport($id);
        $order_data = $this->SomeClientModel->getOrderData($id);
        $region = $this->ClientModel->Region();
        $city = $this->city($client_data[0]['region']);
        $this->crud->assign('client_data', $client_data)
        ->assign('report_data', $report_data)
        ->assign('order_data', $order_data)
        ->assign('region', $region)
        ->assign('city', $city);
        if($printclient ==false)
        {
            $this->crud->display('someclient.tpl');
        }
        else
        {
            $this->crud->display('module/printclient.tpl');
        }
    }
    
    public function city($region=false)
    {
        if($region == false)
        {
            die(json_encode($this->SomeClientModel->City($this->input->post())));
        }
        else
        {
            return $this->SomeClientModel->City($region);
        }
    }
    
    public function insertdata()
    {
        $data = $this->input->post();
        $id = $data['id'];
        unset($data['id']);
        die($this->SomeClientModel->update_post('tb_client', 'id', $id, $data));
    }
    
    public function newbookexcel($id)
    {
       echo $this->SomeClientModel->clientToExcel($id);
    }
     
 /* ==============Стандартное создание ecxel документа========================*/    
    public function excel()
    {
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Disposition: attachment;filename=list.xls" ); 
        header("Content-Transfer-Encoding: binary");
        
        $this->xlsBOF(); //начинаем собирать файл
        /*первая строка*/
        $this->xlsWriteLabel(1,0,"Название");
        /*вторая строка*/
        $this->xlsWriteLabel(2,0,"№п/п");
        $this->xlsWriteLabel(2,1,"Имя");
        $this->xlsWriteLabel(2,2,"Фамилия");
        /*третья строка*/
        $this->xlsWriteNumber(3,0,"1");
        $this->xlsWriteLabel(3,1,"Петр");
        $this->xlsWriteLabel(3,2,"Иванов");
        /*...*/
        $this->xlsWriteNumber(32,0,"30");
        $this->xlsWriteLabel(32,1,"Иван");
        $this->xlsWriteLabel(32,2,"Петров");

        $this->xlsEOF(); //заканчиваем собирать
    }
       
    function xlsBOF() 
    {
        echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
        return;
    }

    function xlsEOF() {
        echo pack("ss", 0x0A, 0x00);
        return;
    }

    function xlsWriteNumber($Row, $Col, $Value) {
        echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
        echo pack("d", $Value);
        return;
    }

    function xlsWriteLabel($Row, $Col, $Value ) {
        $L = strlen($Value);
        echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
        echo $Value;
        return;
    }
}